package band.wukong.practice.springboot.boot.web;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author wukong(wukonggg@139.com)
 */
@RestController
@RequestMapping(value = "/persons")
@SpringBootApplication
public class PersonRestfulApplication {

    private static final Logger logger = LoggerFactory.getLogger(PersonRestfulApplication.class);

    private static final String RESP_SUCCESS = "SUCCESS";

    private static final Map<Long, Person> persons = Collections.synchronizedMap(new HashMap<Long, Person>());

    static {
        persons.put(1L, new Person(1L, "A", 10));
        persons.put(2L, new Person(2L, "B", 20));
        persons.put(3L, new Person(3L, "C", 30));
    }


    @ApiOperation(value = "获取Person列表", notes = "")
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public List<Person> getPersons() {
        logger.debug("GET");

        // 处理"/persons/"的GET请求，用来获取用户列表
        // 还可以通过@RequestParam从页面中传递参数来进行查询条件或者翻页信息的传递
        return new ArrayList<>(persons.values());
    }

    @ApiOperation(value = "创建Person", notes = "根据Person对象创建用户")
    @ApiImplicitParam(name = "p", value = "Person对象", required = true, dataType = "Person")
    @RequestMapping(value = "/", method = RequestMethod.POST)
    public String postPerson(@RequestBody Person p) {
        logger.debug("POST");

        // 处理"/persons/"的POST请求，用来创建User
        // 除了@ModelAttribute绑定参数之外，还可以通过@RequestParam从页面中传递参数
        persons.put(p.getId(), p);
        return RESP_SUCCESS;
    }

    @ApiOperation(value = "获取Person详情", notes = "根据person id获取Person详情")
    @ApiImplicitParam(name = "id", value = "Person ID", required = true, dataType = "Long")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Person getPerson(@PathVariable Long id) {
        logger.debug("GET {id}");

        // 处理"/persons/{id}"的GET请求，用来获取url中id值的User信息
        // url中的id可通过@PathVariable绑定到函数的参数中
        return persons.get(id);
    }

    @ApiOperation(value = "更新person详情", notes = "根据id找到对象，然后根据传过来的person信息来更新person对象详情")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "Person ID", required = true, dataType = "Long"),
        @ApiImplicitParam(name = "p", value = "Person对象", required = true, dataType = "Person")
    })
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public String putPerson(@PathVariable Long id, @RequestBody Person p) {
        logger.debug("PUT {id} {Person}");

        // 处理"/persons/{id}"的PUT请求，用来更新Person信息
        Person person = persons.get(id);
        person.setName(p.getName());
        person.setAge(p.getAge());
        persons.put(person.getId(), person);
        return RESP_SUCCESS;
    }

    @ApiOperation(value = "删除Person", notes = "根据id删除Person对象")
    @ApiImplicitParam(name = "id", value = "Person ID", required = true, dataType = "Long")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public String deletePerson(@PathVariable Long id) {
        logger.debug("DELETE {id}");

        // 处理"/persons/{id}"的DELETE请求，用来删除User
        persons.remove(id);
        return RESP_SUCCESS;
    }


    public static void main(String[] args) {
        SpringApplication.run(PersonRestfulApplication.class, args);
    }
}

/*

@Controller：      修饰class，用来创建处理http请求的对象
@RestController：  Spring4之后加入的注解，原来在@Controller中返回json需要@ResponseBody来配合，
                   如果直接用@RestController替代@Controller就不需要再配置@ResponseBody，默认返回json格式。
@RequestMapping：  配置url映射

 */